package cn.wolfcode.luowowo.article.Service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService{
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    public PageInfo query(TravelQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public Long saveOrUpdate(Travel travel) {
        //作者
        //文章摘要
        String content = travel.getTravelContent().getContent();
        if(content.length()>200){
            travel.setSummary(content.substring(0,200));
        }else {
            travel.setSummary(content);
        }

        //最后更新时间
        travel.setLastUpdateTime(new Date());

        if(travel.getId() == null){
            //创建时间
            travel.setCreateTime(new Date());
            //保存游记内容
            travelMapper.insert(travel);

            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        }else{
            travelMapper.updateByPrimaryKey(travel);

            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }
        return travel.getId();
    }

    public void delete(Long id) {
        travelMapper.deleteByPrimaryKey(id);
    }

    public List<Travel> selectAll() {
      return  travelMapper.selectAll();
    }

    public Travel get(Long id) {
       return travelMapper.selectByPrimaryKey(id);
    }

    public List<Travel> getHotTravelTop3(Long id) {
       return travelMapper.getHotTravelTop3( id);
    }

    public TravelContent getContent(Long id) {
       return  travelContentMapper.selectContentById(id);
    }

    public void updateByState(int state, Long id) {
        travelMapper.updateByState(state,id);
    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }
}
